package mahmoud.sigir.index;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

import mahmoud.sigir.parse.DocParser;
import mahmoud.sigir.parse.OhsumedParser;
import mahmoud.sigir.parse.TrecParser;
import mahmoud.utils.*;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.TermVector;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;


public class Indexer 
{
	private DocParser parser;
	private IndexWriter writer;
	private String IndexLocation;
	public Indexer(DocParser DP, String indexPath)
	{
		parser = DP;
		IndexLocation = indexPath;
	}
	public boolean indexExists()
	{
		File f = new File(IndexLocation);
		if(f.exists())
			return true;
		return false;
	}
	public String getIndexLocation()
	{
		return IndexLocation;
	}
	public boolean buildIndex(boolean checkIFIndexExists)
	{
		if(checkIFIndexExists && indexExists())
		{
			System.out.println("Index already exists at:\t"+IndexLocation);
			System.out.println("if you want to rebuild index please delete abovementioned folder");
			return false;
		}
		try 
		{
			writer = new IndexWriter(FSDirectory.open(new File(IndexLocation)), new PorterStemAnalyzer(),true, IndexWriter.MaxFieldLength.UNLIMITED);
			while(parser.hasMoreDocs())
			{
				HashMap<String,SigirDoc> docMap = parser.nextDoc();
				Iterator<String> it =docMap.keySet().iterator();
				while(it.hasNext())
				{
					Document doc = new Document();
					SigirDoc nxt  = docMap.get(it.next());
					doc.add(new Field("docID",nxt.docID,Field.Store.YES,Field.Index.NOT_ANALYZED) );
					doc.add(new Field("realDocID",nxt.docID,Field.Store.YES,Field.Index.NOT_ANALYZED) );
					doc.add(new Field("Title",nxt.title,Field.Store.YES,Field.Index.NOT_ANALYZED) );
					doc.add(new Field("Path",nxt.filePath,Field.Store.YES,Field.Index.NOT_ANALYZED) );
					doc.add(new Field("All",nxt.toString(),Field.Store.YES,Field.Index.ANALYZED,TermVector.WITH_POSITIONS) );
					writer.addDocument(doc);
				}
			}
			writer.close();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public static void main(String[] args)
	{
//		Indexer I = new Indexer(new TrecParser("D:\\dDocuments\\Code\\TREC4\\FT"), "D:\\dDocuments\\Code\\builtIndexes\\TREC-FT\\");
//		Indexer I = new Indexer(new OhsumedParser("D:\\dDocuments\\Code\\ohsumed\\data"), "D:\\dDocuments\\Code\\builtIndexes\\OHSUMED\\");
		Indexer I = new Indexer(new TrecParser("/media/Data/dDocuments/Code/TREC4/FT"), "/media/Data/dDocuments/Code/builtIndexes/TREC-FT/");
//		Indexer I = new Indexer(new OhsumedParser("/media/Data/dDocuments/Code/ohsumed/data"), "/media/Data/dDocuments/Code/builtIndexes/OHSUMED/");
		boolean checkIFIndexExists = false;
		I.buildIndex(checkIFIndexExists);
		System.out.println("Indexing Done Successfully");
		
	}
	

}
